package com.example.xm.ui.home.activity.youhome_fragments.exercise_activity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.example.xm.R;
import com.example.xm.bean.HomeBean.HomeYouHomeExerciseBean;
import com.google.android.material.appbar.AppBarLayout;

import java.net.URL;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Exercise_rlv_Activity extends AppCompatActivity {
    @BindView(R.id.webView)
    WebView webView;
    @BindView(R.id.txt_content)
    TextView txtContent;

    HomeYouHomeExerciseBean.DataBean.ActivitysBean activitysBean;
    @BindView(R.id.img_head_bg)
    ImageView imgHeadBg;
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.img_shared)
    ImageView imgShared;
    @BindView(R.id.appbar)
    AppBarLayout appbar;
    @BindView(R.id.tv_exercise_yijieshu)
    TextView tvExerciseYijieshu;
    @BindView(R.id.tv_exercise_title)
    TextView tvExerciseTitle;
    @BindView(R.id.layout_tags)
    LinearLayout layoutTags;
    @BindView(R.id.tv_guanzhu_exercise)
    TextView tvGuanzhuExercise;
    @BindView(R.id.tv_baoming_exercise)
    TextView tvBaomingExercise;
    @BindView(R.id.tv_minge)
    TextView tvMinge;
    @BindView(R.id.tv_mianfei)
    TextView tvMianfei;
    @BindView(R.id.tv_huodong_data)
    TextView tvHuodongData;
    @BindView(R.id.tv_jiezhi_date)
    TextView tvJiezhiDate;
    @BindView(R.id.tv_exercise_dizhi)
    TextView tvExerciseDizhi;
    @BindView(R.id.iv_dizhi)
    ImageView ivDizhi;
    @BindView(R.id.rl_youhome_info)
    RelativeLayout rlYouhomeInfo;
    @BindView(R.id.tv_text)
    TextView tvText;
    @BindView(R.id.iv_back2)
    ImageView ivBack2;
    @BindView(R.id.txt_title)
    TextView txtTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exercise_rlv_);
        ButterKnife.bind(this);
        initData();
        initView();
    }

    private void initView() {
        //设置数据
        setData();


        //这里面的resource就是fromhtml函数的第一个参数里面的含有的url
        new Thread(new Runnable() {
            @Override
            public void run() {
                Html.ImageGetter imageGetter = new Html.ImageGetter() {
                    public Drawable getDrawable(String source) {
                        Log.i("RG", "source---?>>>" + source);
                        final Drawable[] drawable = {null};
                        final URL[] url = new URL[1];
                        try {
                            url[0] = new URL(source);
                            Log.i("RG", "url---?>>>" + url[0]);
                            drawable[0] = Drawable.createFromStream(url[0].openStream(), ""); // 获取网路图片
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        if (drawable[0] != null) {
                            drawable[0].setBounds(0, 0, drawable[0].getIntrinsicWidth(),
                                    drawable[0].getIntrinsicHeight());
                            Log.i("RG", "url---?>>>" + url[0]);
                            return drawable[0];
                        } else {
                            return null;
                        }
                    }
                };
//                通过html.fromHtml获取到的图文混排的内容
                Spanned spanned = Html.fromHtml(activitysBean.getContent(), imageGetter, null);
                txtContent.post(new Runnable() {
                    @Override
                    public void run() {
                        txtContent.setText(spanned);
                    }
                });
            }
        }).start();
    }

    @SuppressLint({"NewApi", "SetTextI18n"})
    private void setData() {
        tvExerciseTitle.setText(activitysBean.getTitle());
        Glide.with(this).load(activitysBean.getCover()).into(imgHeadBg);
        tvBaomingExercise.setText("已报名：" + activitysBean.getApplyUserCount());
        tvMinge.setText("剩余名额：" + (activitysBean.getMaxUserNumber() - activitysBean.getApplyUserCount()));
        tvHuodongData.setText("活动时间：" + activitysBean.getStartTime() + "————" + activitysBean.getEndTime());
        tvJiezhiDate.setText("报名截止：" + activitysBean.getApplyCutOffTime());
        tvExerciseDizhi.setText("活动地点：" + activitysBean.getLocation());
        //清理布局中的所有子元素
        layoutTags.removeAllViews();
        //动态添加
        for (HomeYouHomeExerciseBean.DataBean.ActivitysBean.ColorTagsBean item : activitysBean.getColorTags()) {
            TextView txt = (TextView) LayoutInflater.from(Exercise_rlv_Activity.this).inflate(R.layout.layout_tag_status, null);
            txt.setText(item.getTagName());
            txt.setTextColor(Color.parseColor(item.getTagColor()));
            layoutTags.addView(txt);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMarginEnd(10);
            txt.setLayoutParams(params);
            //动态修改边框的颜色
            GradientDrawable gradientDrawable = (GradientDrawable) txt.getBackground();
            gradientDrawable.setStroke(2, Color.parseColor(item.getTagColor()));
        }
        imgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ivBack2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initData() {
        if (getIntent().hasExtra("detail")) {
            activitysBean = (HomeYouHomeExerciseBean.DataBean.ActivitysBean) getIntent().getSerializableExtra("detail");
            if (!TextUtils.isEmpty(activitysBean.getContent())) {
                webView.loadDataWithBaseURL(null, activitysBean.getContent(), "text/html", "utf-8", null);
                //用textview显示网页
                txtContent.setMovementMethod(ScrollingMovementMethod.getInstance());
            }
        }
    }
}